//
//  KqUIAssetsManager.m
//  kq_flutter_widgets
//
//  Created by wtb on 2023/4/12.
//

#import "KqUIAssetsManager.h"
#import <Photos/PHPhotoLibrary.h>

static KqUIAssetsManager *instance = nil;
@implementation KqUIAssetsManager

+ (KqUIAssetsManager *)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
    });
    return instance;
}

/**
 * 重写 +allocWithZone 方法，使得在给对象分配内存空间的时候，就指向同一份数据
 */

+ (id)allocWithZone:(struct _NSZone *)zone {
    return [self sharedInstance];
}

- (instancetype)init {
    if (self = [super init]) {
    }
    return self;
}

+ (KqUIAssetAuthorizationStatus)authorizationStatus{
    __block KqUIAssetAuthorizationStatus status;
//    if (@available(iOS 14, *)) {
//        // 请求权限，需注意 limited 权限尽在 accessLevel 为 readAndWrite 时生效
//        PHAccessLevel level = PHAccessLevelReadWrite;
//        PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatusForAccessLevel:level];
//        if (authorizationStatus == PHAuthorizationStatusRestricted || authorizationStatus == PHAuthorizationStatusDenied) {
//            //手动禁止了授权
//            status = GFUIAssetAuthorizationStatusNotAuthorized;
//        } else if (authorizationStatus == PHAuthorizationStatusNotDetermined) {
//            status = GFUIAssetAuthorizationStatusNotDetermined;
//        } else if(authorizationStatus == PHAuthorizationStatusLimited){
//            status = GFUIAssetAuthorizationStatusLimited;
//        }else {
//            status = GFUIAssetAuthorizationStatusAuthorized;
//        }
//
//    }else{
//
        // 获取当前应用对照片的访问授权状态
        PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
        if (authorizationStatus == PHAuthorizationStatusRestricted || authorizationStatus == PHAuthorizationStatusDenied) {
            //手动禁止了授权
            status = KqUIAssetAuthorizationStatusNotAuthorized;
        } else if (authorizationStatus == PHAuthorizationStatusNotDetermined) {
            //还不确定有没有授权
            status = KqUIAssetAuthorizationStatusNotDetermined;
        } else {
            status = KqUIAssetAuthorizationStatusAuthorized;
        }
//    }
    return status;
}

+ (void)requestAuthorization:(void(^)(KqUIAssetAuthorizationStatus status))handler{
//    if (@available(iOS 14, *)) {
//        PHAccessLevel level = PHAccessLevelReadWrite;
//        [PHPhotoLibrary requestAuthorizationForAccessLevel:level handler:^(PHAuthorizationStatus phStatus) {
//            GFUIAssetAuthorizationStatus status;
//            if (phStatus == PHAuthorizationStatusRestricted || phStatus == PHAuthorizationStatusDenied) {
//                status = GFUIAssetAuthorizationStatusNotAuthorized;
//            } else if (phStatus == PHAuthorizationStatusNotDetermined) {
//                status = GFUIAssetAuthorizationStatusNotDetermined;
//            } else if (phStatus == PHAuthorizationStatusLimited) {
//                status = GFUIAssetAuthorizationStatusLimited;
//            } else {
//                status = GFUIAssetAuthorizationStatusAuthorized;
//            }
//            if (handler) {
//                handler(status);
//            }
//        }];
//    }else{
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus phStatus) {
            KqUIAssetAuthorizationStatus status;
            if (phStatus == PHAuthorizationStatusRestricted || phStatus == PHAuthorizationStatusDenied) {
                status = KqUIAssetAuthorizationStatusNotAuthorized;
            } else if (phStatus == PHAuthorizationStatusNotDetermined) {
                status = KqUIAssetAuthorizationStatusNotDetermined;
            } else {
                status = KqUIAssetAuthorizationStatusAuthorized;
            }
            if (handler) {
                handler(status);
            }
        }];
//    }
}

@end
